<?php
/**
 * Created by JetBrains PhpStorm.
 * User: User
 * Date: 7/23/13
 * Time: 2:50 PM
 * To change this template use File | Settings | File Templates.
 */
class UpFile extends CI_Model{
  public function __construct()
  {
    $this->load->database();
  }
  function insert_file($data){
    $infoData = array(
      'name' => $data['upload_data']['raw_name'] ,
      'type' => $data['upload_data']['file_ext'],
      'size' =>  $data['upload_data']['file_size'],
      'path' => $data['upload_data']['file_path'],
      'idCat' =>  $data['idCat'],
      'timeup' =>  date('d/m/Y'),
      'id_ns' => 'NS_get by sesson'
    );
    $setOk = $this->db->insert('tbl_file', $infoData);
  }
  function update_file($id,$data){
    $new_data = $data; //bien doi cho phu hop
    $this->db->where('id', $id);
    $this->db->update('tbl_file',$new_data);
   }
  function delete_file($id){
    $this->db->delete('tbl_file', array('id' => $id));
  }
  function setCat($tbl,$info){//get info category
    $isOK = $this->db->insert($tbl, $info);
    if($isOK == true){
      return $this->db->insert_id();
    }

  }
  function updateCat($idCat,$nameCat,$parent ='1',$order){
    $dataCat = array(
      'nameCat'=>$nameCat,
      'parent'=>$parent,
      'order'=>$order);
    $this->db->where('idCat',$idCat);
    $this->db->update('tbl_category',$dataCat);
  }
  function deleteCat($idCat,$tbl){
    switch($tbl){
      case 'tbl_support':
        $this->db->where('id',$idCat);
        break;
      default:
        $this->db->where('idCat',$idCat);
        break;
    }
    return $this->db->delete($tbl);
  }
  function getInfo($parent,$tbl,$offset=20,$start=0){
    switch ($tbl){
      case 'tbl_category':
        $this->db->where('parent',$parent);
        $query = $this->db->get($tbl);
        break;
      case 'tbl_file':
        $this->db->where_in('idCat',$parent);
        $this->db->order_by("id", "desc");
        $query = $this->db->get($tbl,$offset,$start);
        break;
    }
    return $query->result();
  }
  function getItem($id,$tbl){
    if($tbl == 'tbl_category'){
      $this->db->where('idCat',$id);
    }else{
      $this->db->where('id',$id);
    }
    $query= $this->db->get($tbl);
    return $query->result();
  }
  function getmenu(){
    $query = $this->db->query('SELECT b1.*,b2.idCat as IDparent,b2.nameCat as name1,b2.parent as parent1  FROM  `tbl_category` b1 left JOIN  `tbl_category`  b2 ON b1.parent = b2.idCat where b1.idCat !=1');
    return $query->result();
  }
}